/*
 * @Description:
 * @Author: Cxy
 * @Date: 2023-04-28 16:07:39
 * @LastEditors: Cxy
 * @LastEditTime: 2023-07-11 15:12:19
 * @FilePath: \giteeBlog\blogAdmin\play\src\stores\user.ts
 */
import { defineStore } from 'pinia'
import router from '../router/index'
import { powerStore } from './power'
import store from './index'
import { cookieRead, cookieRemove, cookieSet } from '@/utils/storage'

export const useUser = defineStore('user', () => {
  const userInfo = reactive({
    expires: cookieRead('token') || 0,
    loginCode: ''
  })
  const login = async (data: any) => {
    const { loginCode } = data
    userInfo.expires = 10
    userInfo.loginCode = loginCode
    cookieSet('token', loginCode, 60 * 60)
    await powerStore.getPermissRouter()
  }

  const loginOut = () => {
    cookieRemove('token')
    router.push('/login')
  }

  return { userInfo, login, loginOut }
})

export const userStore = useUser(store)
